<?php
namespace app\controllers\api\comment;

use fate\http\Request;
use app\models\CommentCenter;
use app\models\User;
use app\libs\LibUser;

class ListController extends \fate\web\PureController {

    public function run() {
        $req = Request::getInstance();

        $page = $req->getQueryString('page');
        $project_id = $req->getQueryString('project_id');
        $post_id = $req->getQueryString('postid');

        if(!is_numeric($page)) {
            $page = 1;
        }
        if(!is_numeric($project_id)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }
        if(!is_numeric($post_id)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        $center = new CommentCenter();
        $pageSize = 20;

        $where = 'status=1 and project_id='. $project_id .' and parent_id=0 and project_inner_id=' . $post_id;
        $total = $center->count($where);
        $data = $center->getListByPage(
            'id,project_id,project_inner_id,parent_id,uid,post_time,status,content',
            $where, $page, $pageSize);

        if(!$data || 0 === count($data)) {
            $this->ajaxReturn([
                'data' => null,
                'total' => $total,
                'current' => 1,
                'page_size' => $pageSize
            ]);
            exit;
        }

        // 有数据
        $userModel = new User();
        $uidsMap = [];
        $subUidsMap = [];
        $userInfoCache = [];
        $replyPageSize = 5;
        for($i=0, $len=count($data); $i<$len; $i++) {
            $uidsMap[ 'u' . $data[$i]['uid'] ] = $data[$i]['uid'];
        }
        $userInfo = $userModel->getListByCondition(
            'id, user_name, avatar',
            'id in(' . implode(',', array_values($uidsMap)) . ')'
        );
        for($i=0, $len=count($userInfo); $i<$len; $i++) {
            $userInfoCache[ 'u' . $userInfo[$i]['id'] ] = $userInfo[$i];
        }


        for($i=0, $len=count($data); $i<$len; $i++) {
            $userInfo = $userInfoCache[ 'u' . $data[$i]['uid'] ];

            $data[$i]['avatar'] = $userInfo['avatar'];
            $data[$i]['user_name'] = $userInfo['user_name'];
            $data[$i]['str_uid'] = LibUser::encodeId($data[$i]['uid']);

            // 子类
            $data[$i]['replies'] = $center->getListByCondition(
                'id,parent_id,uid,content,post_time',
                'status=1 and parent_id = ' . $data[$i]['id'],
                $replyPageSize
            );

            if($data[$i]['replies']) {
                $data[$i]['total_replies'] = $center->count('status=1 and parent_id = ' . $data[$i]['id']);
                //$data[$i]['page_size_replies'] = $replyPageSize;
                $data[$i]['has_more_replies'] = $data[$i]['total_replies'] > $replyPageSize;

                foreach($data[$i]['replies'] as $k => $val) {
                   $subUidsMap[ 'u' . $data[$i]['replies'][$k]['uid'] ] = $data[$i]['replies'][$k]['uid'];
                }
                $userInfo = $userModel->getListByCondition(
                    'id, user_name, avatar',
                    'id in(' . implode(',', array_values($subUidsMap)) . ')'
                );
                foreach($userInfo as $val) {
                    $userInfoCache['u' . $val['id']] = $val;
                }

                foreach($data[$i]['replies'] as $k => $val) {
                    $userInfo = $userInfoCache['u' . $val['uid']];

                    $data[$i]['replies'][$k]['avatar'] = $userInfo['avatar'];
                    $data[$i]['replies'][$k]['user_name'] = $userInfo['user_name'];
                    $data[$i]['replies'][$k]['str_uid'] = LibUser::encodeId($data[$i]['replies'][$k]['uid']);
                }
            }
        }

        $this->ajaxReturn([
            'data' => $data,
            'total' => $total,
            'current' => $page,
            'page_size' => $pageSize
        ]);
    }

}
